
var FileUploadController = function($scope, $upload, $http){
	
	$http.defaults.headers.post["Content-Type"] = "application/json";
	var urlBase="http://localhost:8080/angularjs";
	
	$scope.percent = '';
	
	$scope.dataUrls = [];
	
	$scope.onFileSelect = function($files){
		var $filenew = $files[0];
		if(window.FileReader && $filenew.type.indexOf('image') > -1){
			var fileReader = new FileReader();
			fileReader.readAsDataURL($filenew);
			
			fileReader.onload = function(e){
				$scope.dataUrls['image'] = e.target.result;
			}
		}
		
		 //$files: an array of files selected, each file has name, size, and type.
		
		for (var i = 0; i < $files.length; i++) {
			var file = $files[i];
			$scope.upload = $upload.upload({
				url: urlBase + '/file/upload',
			}).progress(function(evt){
				$scope.percent = parseInt(100.0 * evt.loaded / evt.total);
				 console.log('percent: ' +  $scope.percent);
			}).success(function(respone){
				console.log("upload finished");
			}).error(function(){
				console.log("upload failed");
			});
		}
	};
	
	$scope.sendToDatabase = function(dataUrlsN){
		$http.post(urlBase + '/file/data', dataUrlsN).success(function(respone){
			$scope.dataUrls['imagenew'] = respone;
		}).error(function(){
			alert("failed");
		})
	}
	
}